package visualizeData;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.Rectangle;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

public class VisualizeErrorPlots extends JPanel {

	public VisualizeErrorPlots(String chartName, String xLabel, String yLabel) {
		super();		
		
		this.setLayout(new GridLayout(2,2));
		int hiddenLayers = 1;
		int hiddenNeurons = 4;
		int totalNeurons = 1+hiddenLayers*hiddenNeurons;
		
		XYSeries series[] = new XYSeries[totalNeurons];
		
		int j =0;
		int size = 500;
		
		series[0] = createErrorSeries(0, 0, "Output Layer");
		System.out.println(" created series:"+0 );
		for (int hl=1;hl<=hiddenLayers;hl++){
			j++;
			for (int hn=0;hn<hiddenNeurons;hn++){
				System.out.println(" created series:"+ (j+hn));
				series[j+hn] = createErrorSeries(hl, hn, "Hidden Layer:"+hn);				
			}
		}
				
		XYSeriesCollection dataset[] = new XYSeriesCollection[totalNeurons]; 
		ChartPanel chartPanel[] = new ChartPanel[totalNeurons];
		JFreeChart chart[] = new JFreeChart[totalNeurons];
		
		
		for (int n=0;n<totalNeurons;n++){
			System.out.println(" series length:"+n);
			dataset[n] = new XYSeriesCollection();
			dataset[n].addSeries(series[n]);			
			chart[n] = ChartFactory.createXYLineChart(series[n].getDescription(), xLabel,
					yLabel, dataset[n], PlotOrientation.VERTICAL, true, true, true);
			chartPanel[n] = new ChartPanel(chart[n], size, size, size, size,
					size, size, true, true, true, true, true, true, true);
			add(chartPanel[n]);
		}		
		

	
	}
	
	private XYSeries createErrorSeries(int n1, int n2, String seriesName) {
		String fFileName = "Log/neuralnetworks.log";
		XYSeries series = new XYSeries(seriesName);

		try {
			Scanner scanner = new Scanner(new FileInputStream(fFileName));

			while (scanner.hasNextLine()) {
				String m1 = scanner.nextLine();

				if ((m1.contains("Output Layer") || m1.contains("Hidden Layer")) && m1.contains("INPUT:  [1.0, 0.0]")) {
					String[] m3 = m1.split("\\|");
					String[] m2 = m3[2].split(",");

					if (Integer.valueOf(m2[1]) == n1
							&& Integer.valueOf(m2[2]) == n2) {
//						System.out.println(Integer.valueOf(m2[0]) + ","
//								+ Double.valueOf(m2[3]));
						series.add(Integer.valueOf(m2[0]),
								Double.valueOf(m2[3]));
					}
				}
			}
			scanner.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return series;

	}

	public static void main(String[] args) {

		JPanel panel = new JPanel();
		// panel.setBounds(new Rectangle(1000,1024));
		JFrame frame = new JFrame("Algorithm Performance Stats");


		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		frame.add(panel);
		//
		// frame.add(scrollBar);
		VisualizeErrorPlots VeP =new VisualizeErrorPlots("Weigth Error Data", "xLabel",
				"yLabel");
		// p.setVisible(true);
		JScrollPane scrollBar=new
		JScrollPane(VeP,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
		
		frame.getContentPane().add(
				scrollBar, BorderLayout.CENTER);
		

		
		frame.pack();
		frame.setVisible(true);
	}
}